// WebSocket消息响应模型
class WebSocketMessageResponse {
  final String type;
  final List<NoticeMessageModel>? data;

  WebSocketMessageResponse({required this.type, this.data});

  factory WebSocketMessageResponse.fromJson(Map<String, dynamic> json) {
    return WebSocketMessageResponse(
      type: json['type'] ?? '',
      data: json['data'] != null && json['data'] is List
          ? (json['data'] as List)
              .map((item) => NoticeMessageModel.fromJson(item))
              .toList()
          : (json['data'] != null
              ? [NoticeMessageModel.fromJson(json['data'])]
              : null),
    );
  }
}

// 通知消息模型
class NoticeMessageModel {
  final int id;
  final int type;
  final NotificationContent title;
  final NotificationContent content;
  final ContentData data;
  int status;
  final DateTime createdAt;

  NoticeMessageModel({
    required this.id,
    required this.type,
    required this.title,
    required this.content,
    required this.data,
    required this.status,
    required this.createdAt,
  });

  factory NoticeMessageModel.fromJson(Map<String, dynamic> json) {
    return NoticeMessageModel(
      id: json['id'],
      type: json['type'],
      title: NotificationContent.fromJson(json['title']),
      content: NotificationContent.fromJson(json['content']),
      data: ContentData.fromJson(json['data'] ?? {}),
      status: json['status'] ?? 0,
      createdAt: DateTime.parse(json['created_at']).toLocal(),
    );
  }

  // toJson
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'type': type,
      'title': {
        'en': title.en,
        'zh': title.zh,
      },
      'content': {
        'en': content.en,
        'zh': content.zh,
      },
      'data': {
        'event_data': data.eventData,
        'event_type': data.eventType,
        'sn': data.sn,
        'device_name': data.deviceName,
      },
      'status': status,
      'created_at': createdAt.toIso8601String(),
    };
  }

  // toString
  @override
  String toString() {
    return 'NotificationData(id: $id, type: $type, title: $title, content: ${content.en} / ${content.zh}, data: $data, status: $status, createdAt: $createdAt)';
  }
}

class NotificationContent {
  final String en;
  final String zh;

  NotificationContent({required this.en, required this.zh});

  factory NotificationContent.fromJson(Map<String, dynamic> json) {
    return NotificationContent(
      en: json['en'] ?? '',
      zh: json['zh'] ?? '',
    );
  }
}

class ContentData {
  // "data":{"event_data":"","event_type":2,"sn":"A0311871400018CC"},

  final String eventData;
  final int eventType;
  final String sn;
  final String deviceName;

  ContentData({
    required this.eventData,
    required this.eventType,
    required this.sn,
    required this.deviceName,
  });

  factory ContentData.fromJson(Map<String, dynamic> json) {
    return ContentData(
      eventData: json['event_data'] ?? '',
      eventType: json['event_type'] ?? 0,
      deviceName: json['device_name'] ?? '',
      sn: json['sn'] ?? '',
    );
  }

  // toString
  @override
  String toString() {
    return 'MessageData(eventData: $eventData, eventType: $eventType, sn: $sn, deviceName: $deviceName)';
  }
}
